package com.tx.java.chapter14.exercise14_6;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 测试反射
 */
public class Test {
    public static void main(String[] args) {
        try {
            Class clazz = Class.forName("com.tx.java.chapter14.exercise14_6.BaseClass");
            /*testConstrutor(clazz); //测试构造方法*/
            /*testMethod(clazz);  //测试普通方法*/
            testField(clazz); // 测试成员变量
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     *  反射测试成员变量
     * @param clazz
     */
    private static void testField(Class clazz) {
        try {
            Object o =  clazz.getConstructor().newInstance();
//            Field[] fields = clazz.getFields();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field: fields) {
                System.out.println(field);
            }

            Field publicField = clazz.getDeclaredField("publicValue");
            System.out.println("public的值：" + publicField.get(o));
            publicField.set(o,"111");
            System.out.println("更改之后，public的值：" + publicField.get(o));

            //私有属性
            Field privateField = clazz.getDeclaredField("name");
            privateField.setAccessible(true);
            System.out.println("private的值：" + privateField.get(o));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 反射测试调用方法
     * @param clazz
     * @throws NoSuchMethodException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private static void testMethod(Class clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        try {
            Constructor constructor = clazz.getConstructor(null);
            Object o =  constructor.newInstance();
            //1.获取方法
//            Method[] methods = clazz.getMethods(); //还会获取父类中的方法
            Method[] methods = clazz.getDeclaredMethods(); //获取本类中的方法--包括私有
            for (Method m: methods) {
                System.out.println(m);
            }
            //2.调用方法
            //--公共方法
            Method method = clazz.getMethod("publicMethod");
            method.invoke(o); //调用方法
            //--私有方法
            Method privateMethod = clazz.getDeclaredMethod("privateMethod");
            privateMethod.setAccessible(true); //设置对私有方法的访问权限
            privateMethod.invoke(o);
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试构造器方法
     * @param clazz
     * @return
     * @throws NoSuchMethodException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     */
    private static Object testConstrutor(Class clazz) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //----构造器
        Constructor[] constructors = clazz.getConstructors();
        for (Constructor c : constructors) {
            System.out.println(c);
        }
        //获得无参构造器
        Constructor constructor = clazz.getConstructor(null);
        Object o =  constructor.newInstance();

        //实例化有参构造方法
        Object paramCons = clazz.getConstructor(String.class).newInstance("xixi");
        return o;
    }
}